using System;
using System.Collections.Generic;
using System.Text;

namespace BAFHTTPD
{
    public class ErrorPageProvider
    {
        public string TemplateCSS =
            "body { margin: 2; }\n" +
            "h1.header {\n" +
            "   color: #800000;\n" +
            "   font-size: 64;\n" +
            "   font-family: \"BankGothic Md BT\";\n" +
            "}\n" +
            "input { border: 1px inset SlateBlue; }\n" +
            "div.search { font-family: arial; font-size: 12px; }\n" +
            "a { text-decoration: none; }\n" +
            "a.footerlinks { font-size: 14px; color: #800000; }\n" +
            "a.footerlinks:hover { color: SlateBlue; font-weight: bold; }\n";

        public string TemplateHTML =
            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" +
            "<html>\n" +
            " <head>\n" +
            "  <title>{title}</title>\n" +
            "  <meta http-equiv=Content-Type content=\"text/html; charset=windows-1252\">\n" +
            "  {css}\n" +
            " </head>\n" +
            "<body>\n" +
            "<h1 align=center class=\"header\">{title}</h1>\n" +
            "<h3 align=center>{subtitle}</h3>\n" +
            "<div align=\"center\"><font size=-1>{text}</font></div>\n" +
            "<div align=\"center\">\n" +
            //"<a class=\"footerlinks\" href=\"#\">Any</a> - \n" +
            //"<a class=\"footerlinks\" href=\"#\">links</a> - \n" +
            //"<a class=\"footerlinks\" href=\"#\">?</a>\n" +
            "<p><font size=-2>{server}</font></p>\n" +
            "</div>\n" +
            "</body>\n" +
            "</html>\n";

        public string ServerString;

        private string DoPageReplace(string title, string subtitle, string text)
        {
            return TemplateHTML.Replace("{css}", "<style>" + TemplateCSS + "</style>").Replace("{title}", title).Replace("{subtitle}", subtitle).Replace("{text}", text).Replace("{server}", ServerString);
        }

        public int Do400(RequestInfo Request, out string Page, out string ContentType)
        {
            string Requested = Request.Request.Substring(Request.Request.LastIndexOf('/') + 1);
            if (Requested.IndexOf('?') > 0)
                Requested = Requested.Remove(Requested.IndexOf('?'));

            ContentType = "text/html";
            Page = DoPageReplace("400 Bad Request", "Bad Request", "The request could not be understood by the server due to malformed syntax.");
            return 400;
        }

        public int Do403(RequestInfo Request, out string Page, out string ContentType)
        {
            string Requested = Request.Request.Substring(Request.Request.LastIndexOf('/') + 1);
            if (Requested.IndexOf('?') > 0)
                Requested = Requested.Remove(Requested.IndexOf('?'));

            ContentType = "text/html";
            Page = DoPageReplace("403 Forbidden", "Forbidden", "The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated.");
            return 403;
        }

        public int Do404(RequestInfo Request, out string Page, out string ContentType)
        {
            string Requested = Request.Request.Substring(Request.Request.LastIndexOf('/') + 1);
            if (Requested.IndexOf('?') > 0)
                Requested = Requested.Remove(Requested.IndexOf('?'));

            ContentType = "text/html";
            Page = DoPageReplace("404 Not Found", "File Not Found", "The requested document [" + Requested + "] was not found.");
            return 404;
        }
    }
}
